﻿using UnityEngine;
using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Jobs.LowLevel.Unsafe;

namespace DotsGame
{
    //公共数据是为了在多个System 和 Mono 之间穿越的数据

    //我们有很多种办法来制造公共数据
    //1.Static 不优雅
    //2.通过继承SystemBase 的System 作为公共数据，可以随时用world 获取他
    //3.通过Burst 创建一个 SharedStatic 数据，作为公共数据

    //其中方法3的最为干净，我们就用最干净的，其它方法都容易混入一些托管的内容
    //战斗阵营信息


    //表示一个单位
    public struct UnitPos
    {
        public bool enable;
        public float3 pos;
        public float half;
    }

    //一个阵营
    public struct BattleCamp
    {
        public int id;
        public NativeArray<UnitPos> mainTargets;//重要目标,最多十个，立个靶子给别人打
        public int UnitCount;
        public int MainUnitCount;
        public void Init()
        {
            mainTargets = new NativeArray<UnitPos>(10, Allocator.Persistent);
        }
    }
   
    public struct CommonData
    {
        //主要目标，
        public NativeArray<dotsbuf_mesh> config_mesh;
        public NativeArray<dotsbuf_dummy> config_dummy;
        public NativeArray<dotsbuf_effect> config_effect;

        public BattleCamp campA;
        public BattleCamp campB;
        public int EffectCount;
        public NativeRingQueue<CommandToSystem> queueCmdToSystem;
        public bool hasInit;
        public void Init()
        {
            campA = new BattleCamp();
            campB = new BattleCamp();
            campA.Init();
            campB.Init();
            queueCmdToSystem = new NativeRingQueue<CommandToSystem>(1000, Allocator.Persistent);
            hasInit = true;
            Debug.Log("CommonData.hasinit=true");
        }
        static long insthash = 0;
        static long insthash2;
        public static SharedStatic<CommonData> Instance
        {
            get
            {
                if (insthash == 0)
                {
                    insthash = BurstRuntime.GetHashCode64<CommonData>();
                }
                for (var i = 0; i < 10; i++)
                {
                    try
                    {
                        return SharedStatic<CommonData>.GetOrCreateUnsafe(0, insthash, 0);
                    }
                    catch (System.Exception err)
                    {
                        Debug.LogWarning("SharedStatic<CommonData>创不出,换个hash");
                        insthash++;
                    }
                }
                throw new System.Exception("SharedStatic 创不出，这一般是因为Burst编译器对SharedStatic改变的问题，你该重启Unity了");
            }
        }
    }
}
